# SleepMirror - A reflection of your sleep quality
#
# sm_identify.rb
#
# This is the panel that will retrieve identifying information from the Participant.
#
# ©2010 JohnGary, LLC.  All Rights Reserved.
#
#
# Changelog:
#
#
#

require 'sm_panel'
require 'sm_participant'
require "rubygame"

include Rubygame
include Rubygame::Events
include Rubygame::EventActions
include Rubygame::EventTriggers
class Text_box
  include Sprites::Sprite
  #include EventHandler::HasEventHandler
  #DEFAULT_ASSET = "Humphrey_blue_fixation_circle_img.jpg"
  attr_accessor :text

  def initialize (px, py)
    super()
#    this_dir = File.dirname( __FILE__ )
#    file = File.expand_path( DEFAULT_ASSET, this_dir ) # default file
    @image = Surface.new([100,30],1,[HWSURFACE])
    @rect = @image.make_rect
    @rect.center=[px,py]
    @text=" "
  end
  def set_text(text)
    TTF.setup()
    ttfont_path = File.join(File.dirname(__FILE__),"FreeSans.ttf")
    ttfont = TTF.new( ttfont_path, 20 )
    result = ttfont.render( text,true,:gray)
    result.blit(@image,[1,1])
  end
end
class SM_identify < SM_panel
  attr_accessor :screen, :participant
  DEFAULT_MESSAGE_Y = 50

  def initialize(name="", instructions="", asset="")
    super(name, instructions, asset)
    @screen=Screen.new([640,480],0,[HWSURFACE])
    @screen.title=name
    @screen.show_cursor=true
    @participant=nil
    @data=""
    make_magic_hooks(
      KeyReleased => :key_released
    )
    #@echo=Text_box.new(100,100) #Create a new Text_box to hold the echo for @data
    @echo = Rect.new(100,100,300,30)
  end
  def say_this (what="What?")
    @screen.fill :black, @echo #clear the echo area
    if what.length >0
      TTF.setup()
      ttfont_path = File.join(File.dirname(__FILE__),"FreeSans.ttf")
      ttfont = TTF.new( ttfont_path, 20 )
      result = ttfont.render( what,true,:gray)
      #    result.blit( @screen,[@screen.width/2-result.size[0]/2,DEFAULT_MESSAGE_Y])
      #result.blit(@echo,[0,0])
      result.blit(@screen,@echo)
      #@echo.blit(@screen, [100,100])
    end
  end
  def key_released(event)
    #puts "->#{event.key}"
    if event.key==:backspace
      @data.chop! unless @data.length == 0
    elsif event.key==:space
      @data += ' '
    elsif event.key =~ /^number_\d$/         #the number keys are returned as 'number_n'
      @data += event.key[event.key.length-1] #get the last char in the key, which is the numberal itself
    else
      @data += event.key if event.key =~ /^\w$/
    end
    puts @data
    say_this(@data)
    #@echo.set_text(@data)
    #@echo.draw(@screen)
  end
  def perform
    # Setup the event queue for this test
    events=EventQueue.new
    events.enable_new_style_events
    # Loop until we get an answer or an exit.
    loop do
      events = Rubygame::Events.fetch_sdl_events
      events.each do |event|
        #@handler.handle( event )
        handle( event )
      end
      @screen.update
      sleep 0.1
    end
  end
end
